package memento;

import java.io.*;

public class GameRome implements Cloneable, Serializable{

    private String name;
    private int vit;
    private int def;

    public GameRome(String name, int vit, int def) {
        this.name = name;
        this.vit = vit;
        this.def = def;
    }

    //克隆备份
    public GameRome getMemento(){
        GameRome gameRome = null;
        try {
            gameRome = deepClone();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return gameRome;
    }

    public void recoverFromMemento(GameRome gameRome){
        this.vit = gameRome.vit;
        this.def = gameRome.def;
        display();
    }

    public void display() {
        System.out.println(this.name + "当前的攻击力：" + this.vit + " 防御力: " + this.def);
    }

    public GameRome deepClone() throws IOException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        GameRome gameRome = null;
        try(bos;oos;bis;ois){
            gameRome = (GameRome) ois.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return gameRome;
    }

    public int getVit() {
        return vit;
    }

    public void setVit(int vit) {
        this.vit = vit;
    }

    public int getDef() {
        return def;
    }

    public void setDef(int def) {
        this.def = def;
    }
}
